package com.manor.main.activity

import android.content.Context
import android.graphics.Color
import com.alibaba.android.arouter.facade.annotation.Route
import com.manor.common.activity.AbsActivity
import com.manor.common.utils.DpUtil
import com.manor.common.utils.RouteUtil
import com.manor.main.R
import com.manor.main.adapter.CommonPagerAdapter
import com.manor.main.fragment.KMyGardFragment
import com.manor.main.http.MainHttpConsts
import com.manor.main.http.MainHttpUtil
import kotlinx.android.synthetic.main.view_main_list.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView

/**
 * 我的守护
 */
@Route(path = RouteUtil.PC_MyGuard)
class KMyGuardActivity  : AbsActivity(){
    private var is_shouhu  = true;

    private var guar_id: String = ""
    override fun getLayoutId(): Int = R.layout.activity_myguard;
    val titles = arrayOf( "我守护的", "守护我的")
    override fun main() {
        btn_back.setOnClickListener({finish()})
        val pagerAdapter = CommonPagerAdapter(supportFragmentManager, KMyGardFragment(0), KMyGardFragment(1))
//        ImmersionBar.with(this)
//                .statusBarColor(R.color.title)
//                .init()
        viewPager.setOffscreenPageLimit(0)//加载几页
        viewPager.setAdapter(pagerAdapter)
        val commonNavigator = CommonNavigator(this)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int {
                return titles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = ColorTransitionPagerTitleView(context)
                simplePagerTitleView.normalColor = Color.parseColor("#ffffff")
                simplePagerTitleView.selectedColor = Color.parseColor("#ffffff")
                simplePagerTitleView.setText(titles[index])
                simplePagerTitleView.textSize = 16f
                simplePagerTitleView.paint.isFakeBoldText = true
                simplePagerTitleView.setPadding(DpUtil.dp2px(20), 0, DpUtil.dp2px(20), 0)
                simplePagerTitleView.setOnClickListener {
                    if (viewPager != null) {
                        viewPager.setCurrentItem(index)
                    }
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.setColors(Color.parseColor("#CE2F69"))
                indicator.xOffset =0.5f
                indicator.lineWidth = UIUtil.dip2px(context, 30.0).toFloat()
               indicator.mode = LinePagerIndicator.MODE_EXACTLY

                return indicator
            }
        }
        indicator.setNavigator(commonNavigator)
        ViewPagerHelper.bind(indicator, viewPager)
        viewPager.setCurrentItem(0)
    }
/*    override fun main() {
        super.main()
        setTitle(WordUtil.getString(R.string.my_guard))
        rc_listview.setLayoutManager(androidx.recyclerview.widget.LinearLayoutManager(mContext, androidx.recyclerview.widget.LinearLayoutManager.VERTICAL, false))
        mAdapter = KMyGuardAdapter(R.layout.item_myguard, null)
        rc_listview.setAdapter(mAdapter)
        ll_my_sh.setOnClickListener{
            if(!is_shouhu){
                is_shouhu = true
                tv_my_sh.visibility = View.VISIBLE
                tv_sh_my.visibility = View.GONE
                tv_shw_view.setBackgroundResource(R.mipmap.pic_hui)
                tv_sh_view.setBackgroundResource(R.mipmap.pic_cai)
                setData()
            }
        }
        ll_sh_my.setOnClickListener {
            if (is_shouhu) {
                is_shouhu = false
                tv_my_sh.visibility = View.GONE
                tv_sh_my.visibility = View.VISIBLE
                tv_shw_view.setBackgroundResource(R.mipmap.pic_cai)
                tv_sh_view.setBackgroundResource(R.mipmap.pic_hui)
                setData()
            }
        }
        setData()
    }

    private fun setData() {
        if (is_shouhu) {
            guar_id = ""
        } else {
            guar_id = CommonAppConfig.getInstance().uid
        }
        MainHttpUtil.GetGuardList(CommonAppConfig.getInstance().uid, CommonAppConfig.getInstance().token,
                guar_id + "", object : HttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: Array<String>?) {
                if (code == 0 && info != null) {
                    if (info.size > 0) {
                        val myGuardBean = JSON.parseArray(Arrays.toString(info), MyGuardBean::class.java)
                        //   WithdrawalAccountBean withdrawalAccountBean = mGson.fromJson(info[0], WithdrawalAccountBean.class);
                        mAdapter.setNewInstance(myGuardBean)
                    } else {
                        mAdapter.setNewInstance(null)
                        mAdapter.setEmptyView(R.layout.view_no_data_dynamic4)
                    }


                } else {
                    mAdapter.setNewInstance(null)
                    ToastUtil.show(msg)
                }
            }
        })
    }*/
    public override fun onDestroy() {
        MainHttpUtil.cancel(MainHttpConsts.IOSTOKEN)

        super.onDestroy()
    }

}